package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.service.MarketAccountService
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("markets/{marketId}/accounts")
class MarketsAccountController(
    private val marketAccountService: MarketAccountService
) {

    data class MarketAccountResponse(
        val name: String,
        val id: Int,
    )

    @GetMapping
    fun getMarketAccountsByMarketId(@PathVariable marketId: Int): List<MarketAccountResponse> {
        return marketAccountService.getMarketAccountsByMarketId(marketId)
    }

    data class CreateMarketAccountRequest(
        val name: String,
    )

    @PostMapping
    fun createMarketAccount(
        @PathVariable marketId: Int,
        @RequestBody request: CreateMarketAccountRequest
    ): Int {
        return marketAccountService.createMarketAccount(marketId, request).id!!
    }

    @PutMapping("{id}")
    fun updateMarketAccount(
        @PathVariable marketId: Int, @RequestBody request: CreateMarketAccountRequest,
        @PathVariable id: Int
    ) {
        marketAccountService.updateMarketAccount(marketId, request, id)
    }

    @DeleteMapping("{id}")
    fun deleteMarketAccount(@PathVariable marketId: Int, @PathVariable id: Int) {
        marketAccountService.deleteMarketAccount(marketId, id)
    }

}